package org.libarae.communication.common;

import java.awt.image.BufferedImage;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Book extends Common {
	//Used to set status
	public static enum BookStatus {
	    CHECKED_OUT { public String toString() { return "Checked Out"; } },
        PENDING { public String toString() { return "Pending"; } },
        AVAILABLE { public String toString() { return "Available"; } }
	};
	
	private int mBID;
	private int mOwnerID;
	private int mCheckoutCount;
	private int mCheckedOutBy;	//-1 means it is not checked out, else it will = the userID of the check

	private String mTitle = "";
	private String mISBN = "";
	private String mAuthor = "";
	private String mPubDate = "";
	private String mAddedDate = "";
	private String mSummary = "";
	private String mPublisher = "";	
	private BookStatus mBookStatus;
	
	//for the image, if null the picture must be requested using a PictureRequest
	private Picture mPicture = new Picture();
	
	//creates a new book object
	public Book(){
	}
	
	public Book(int bookID, int ownerID, int checkOutCount, String title, String isbn, String author, String pubDate, String addedDate, String summary, String publisher, BookStatus status, int checkedOutBy, BufferedImage image) {
		setBID(bookID);
		setOwnerID(ownerID);
		setCheckoutCount(checkOutCount);
		setTitle(title);
		setISBN(isbn);
		setAuthor(author);
		setPublisher(publisher);
		setBookStatus(status);
		setCheckedOutBy(checkedOutBy);
		setPicture(image);
	}
	
	
	@Override
	public void decodeXML(Node root) {
		NodeList nodeList = root.getChildNodes();

		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			if (nodeName.equals("mBID")) {
				mBID = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mOwnerID")) {
				mOwnerID = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mCheckoutCount")) {
				mCheckoutCount = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mCheckedOutBy")) {
				mCheckedOutBy = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mTitle")) {
				mTitle = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mISBN")) {
				mISBN = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mAuthor")) {
				mAuthor = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mPubDate")) {
				mPubDate = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mAddedDate")) {
				mAddedDate = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mSummary")) {
				mSummary = nodeList.item(i).getTextContent();
			}			
			else if (nodeName.equals("mPublisher")) {
				mPublisher = nodeList.item(i).getTextContent();
			}			
			else if (nodeName.equals("mBookStatus")) {
				switch(Integer.parseInt(nodeList.item(i).getTextContent())){
					case -1: //it didn't exist
						break;
					case 0:
						mBookStatus = BookStatus.CHECKED_OUT;
					break;
					case 1:
						mBookStatus = BookStatus.PENDING;
					break;
					case 2:
						mBookStatus = BookStatus.AVAILABLE;
					break;
				}
			}		
			else if (nodeName.equals("mPicture")) {
				mPicture = new Picture();
				mPicture.decodeXML(nodeList.item(i));
			}
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}		
	}
	
	
	@Override
	public void createXml(Document doc, Element root) {
		Element node = doc.createElement("mBID");
		node.setTextContent(mBID+"");
		root.appendChild(node);
		
		node = doc.createElement("mOwnerID");
		node.setTextContent(mOwnerID+"");
		root.appendChild(node);
		
		node = doc.createElement("mCheckoutCount");
		node.setTextContent(mCheckoutCount+"");
		root.appendChild(node);
		
		node = doc.createElement("mCheckedOutBy");
		node.setTextContent(mCheckedOutBy+"");
		root.appendChild(node);
		
		node = doc.createElement("mTitle");
		node.setTextContent(mTitle);
		root.appendChild(node);
		
		node = doc.createElement("mISBN");
		node.setTextContent(mISBN);
		root.appendChild(node);
		
		node = doc.createElement("mAuthor");
		node.setTextContent(mAuthor);
		root.appendChild(node);
		
		node = doc.createElement("mPubDate");
		node.setTextContent(mPubDate);
		root.appendChild(node);
		
		node = doc.createElement("mAddedDate");
		node.setTextContent(mAddedDate);
		root.appendChild(node);
		
		node = doc.createElement("mSummary");
		node.setTextContent(mSummary);
		root.appendChild(node);
		
		node = doc.createElement("mPublisher");
		node.setTextContent(mPublisher);
		root.appendChild(node);
		
		node = doc.createElement("mBookStatus");
		//if it exists then use the number
		if(mBookStatus != null)
			node.setTextContent(mBookStatus.ordinal()+"");
		else
		//if not set it to be null
			node.setTextContent("-1");
		root.appendChild(node);		
		
		//if the image is not null set it
		if(mPicture.getmImage() != null){
			node = doc.createElement("mPicture");
			mPicture.createXml(doc, node);
			root.appendChild(node);		
		}
	}
	
	//sets all of the information for a book
	public void setBook(int bookID, int ownerID, int checkOutCount, String title, String isbn, String author, String pubDate, String addedDate, String summary, String publisher, BookStatus status, int checkedOutBy) {
		this.mBID = bookID;
		this.mOwnerID = ownerID;
		this.mCheckoutCount = checkOutCount;
		this.mTitle = title;
		this.mISBN = isbn;
		this.mAuthor = author;
		this.mPubDate = pubDate;
		this.mAddedDate = addedDate;
		this.mSummary = summary;
		this.mPublisher  = publisher;
		this.mBookStatus = status;
		this.mCheckedOutBy = checkedOutBy;
	}	
	
	public int getCheckedOutBy() {
		return mCheckedOutBy;
	}

	public void setCheckedOutBy(int checkedOutBy) {
		this.mCheckedOutBy = checkedOutBy;
	}
	public int getOwnerID() {
		return mOwnerID;
	}

	public void setOwnerID(int ownerID) {
		this.mOwnerID = ownerID;
	}
	
	
	public int getBID() {
		return mBID;
	}

	public void setBID(int mBID) {
		this.mBID = mBID;
	}

	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String mTitle) {
		this.mTitle = mTitle;
	}

	public String getISBN() {
		return mISBN;
	}

	public void setISBN(String mISBN) {
		this.mISBN = mISBN;
	}

	public String getAuthor() {
		return mAuthor;
	}

	public void setAuthor(String mAuthor) {
		this.mAuthor = mAuthor;
	}

	public String getPubDate() {
		return mPubDate;
	}

	public void setPubDate(String mPubDate) {
		this.mPubDate = mPubDate;
	}

	public String getAddedDate() {
		return mAddedDate;
	}

	public void setAddedDate(String mAddedDate) {
		this.mAddedDate = mAddedDate;
	}

	public String getSummary() {
		return mSummary;
	}

	public void setSummary(String mSummary) {
		this.mSummary = mSummary;
	}

	public String getPublisher() {
		return mPublisher;
	}

	public void setPublisher(String mPublisher) {
		this.mPublisher = mPublisher;
	}

	public BookStatus getBookStatus() {
		return mBookStatus;
	}

	public void setBookStatus(BookStatus mBookStatus) {
		this.mBookStatus = mBookStatus;
	}

	public BufferedImage getPicture() {
		return mPicture.getmImage();
	}

	public void setPicture(BufferedImage image) {
		this.mPicture = new Picture(image);
	}
	
	public int getCheckoutCount() {
		return mCheckoutCount;
	}

	public void setCheckoutCount(int mCheckoutCount) {
		this.mCheckoutCount = mCheckoutCount;
	}
}
